package com.whli.jee.main.service.impl;

import com.whli.jee.common.constant.SysConstants;
import com.whli.jee.common.exception.BusinessException;
import com.whli.jee.common.util.BeanUtils;
import com.whli.jee.common.util.JedisUtils;
import com.whli.jee.common.util.PwdUtils;
import com.whli.jee.common.util.WebUtils;
import com.whli.jee.common.web.service.IBaseAuthenticationService;
import com.whli.jee.system.entity.SysUser;
import com.whli.jee.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>系统获取类</p>
 * @author whli
 * @version 1.0.0
 * @date 2019/6/23 20:51
 */
@Service("baseAuthenticationService")
public class MainAuthenticationService implements IBaseAuthenticationService {

    @Autowired
    private ISysUserService userService;

    @Override
    public Map<String, Object> login(HttpServletRequest request) {
        String loginName = request.getParameter("username");
        String password = request.getParameter("password");
        String officeId = request.getParameter("officeId");
        if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)){
            throw new BusinessException("用户名或密码不能为空！");
        }

        SysUser sysUser = userService.getByLoginNameOrEmailOrPhone(loginName);
        if (BeanUtils.isNull(sysUser)) {
            throw new BusinessException("用户名或密码错误！");
        }
        if (!sysUser.getPassword().equals(PwdUtils.md5Encode(password, loginName))) {
            throw new BusinessException("用户名或密码错误！");
        }

        String enabled = sysUser.getEnable();
        if (StringUtils.isBlank(enabled) || !"1".equals(enabled)) {
            throw new BusinessException("该用户已删除或禁止登录，请联系管理人员！");
        }

        //增加token
        Map<String,String> values = new HashMap<>();
        String token = BeanUtils.getUUID();
        values.put(SysConstants.LOGIN_USER_NAME,sysUser.getLoginName());
        values.put(SysConstants.LOGIN_USER_ID,sysUser.getId());
        if (StringUtils.isNotBlank(officeId)){
            values.put("office_id",officeId);
        }
        JedisUtils.hMSet(token,values,0);

        //增加当前IP
        JedisUtils.hSet(SysConstants.KICTOUT,SysConstants.KICTOUT_PC_PREFIX+sysUser.getLoginName(),token,0);

        Map<String,Object> result = new HashMap<>();
        result.put("token",token);
        result.put("loginName",loginName);
        result.put("loginId",sysUser.getId());
        return result;
    }

    @Override
    public boolean logout(HttpServletRequest request) {
        String token = request.getHeader(SysConstants.AUTHORIZATION);
        if (StringUtils.isBlank(token)){
            return false;
        }
        return JedisUtils.delete(token);
    }


    @Override
    public String getLoginUserName() {
        String token = getToken();
        if (StringUtils.isBlank(token)){
            return null;
        }
        return JedisUtils.hGet(token, SysConstants.LOGIN_USER_NAME);
    }


    @Override
    public String getLoginUserId() {
        String token = getToken();
        if (StringUtils.isBlank(token)){
            return null;
        }
        return JedisUtils.hGet(token, SysConstants.LOGIN_USER_ID);
    }


    @Override
    public boolean existsToken(String token) {
        if (StringUtils.isBlank(token)){
            return false;
        }
        return JedisUtils.exists(token);
    }

    @Override
    public String getToken() {
        HttpServletRequest request = WebUtils.getRequest();
        if (request == null){
            return  null;
        }

        String token = (String)request.getHeader(SysConstants.AUTHORIZATION);
        if(!existsToken(token)) {
            return null;
        }
        return token;
    }

}
